package com.dlry.web;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dlry.entity.DlLoginuser;
import com.dlry.entity.DlNotification;
import com.dlry.serviceImpl.NotificationService;
import com.dlry.serviceImpl.UserService;

@Controller
public class NotificationController extends BaseController {
	@Autowired
	private NotificationService notificationService;
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/notification", method = RequestMethod.GET)
	public String readOneNotification(@RequestParam(name = "nId") Long nId, Model model) {
		DlNotification notification = notificationService.findOne(nId);
		DlLoginuser duser = notification.getUser();
		model.addAttribute("notification", notification);
		model.addAttribute("writer", duser.getRealName());
		model.addAttribute("title", notification.getTitle());
		String name = getPrincipal();
		if (name != null) {
			DlLoginuser duser1 = userService.findByName(name);
			model.addAttribute("duser", duser1);
		}
		return "onenews";
	}

	@RequestMapping("/{roler}/addnotification")
	public String addNotification(Model model, @PathVariable("roler") String roler) {
		String name = getPrincipal();
		DlLoginuser user = userService.findByName(name);
		model.addAttribute("duser", user);
		model.addAttribute("roler", roler);
		model.addAttribute("notification", new DlNotification());
		model.addAttribute("title", "新发通知");
		return "addnews";
	}

	@RequestMapping(value = "/{roler}/deletenotification")
	public String deleteNtofication(@RequestParam(name = "nId") Long nId,
			@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
			@PathVariable("roler") String roler) {
		notificationService.delete(nId);
		return "redirect:/" + roler + "/notification?current=" + currentPage;
	}

	@RequestMapping(value = "/{roler}/editnotification")
	public String editNotification(@RequestParam(name = "nId") Long nId, Model model,
			@PathVariable("roler") String roler) {
		String name = getPrincipal();
		DlLoginuser user = userService.findByName(name);
		DlNotification notification = notificationService.findOne(nId);
		model.addAttribute("notification", notification);
		model.addAttribute("duser", user);
		model.addAttribute("roler", roler);
		return "editnews";
	}

	@RequestMapping(value = "/{roler}/updatenotification")
	public String updateNotification(@RequestParam(name = "id") Long nId, @RequestParam(name = "title") String title,
			String detail, @RequestParam(name = "toped", defaultValue = "false") Boolean toped,
			@PathVariable("roler") String roler) {
		Boolean ntoped = toped == null ? false : toped;
		DlNotification notification = notificationService.findOne(nId);
		notification.setTitle(title);
		notification.setDetail(detail);
		notification.setToped(ntoped);
		Timestamp updatetamp = new Timestamp(new Date().getTime());
		notification.setUpdatetime(updatetamp);
		notificationService.save(notification);
		return "redirect:/" + roler + "/notification?current=1";
	}

	@RequestMapping("/{roler}/savenotification")
	public String saveNotification(@RequestParam(name = "title") String title,
			@RequestParam(name = "detail") String detail,
			@RequestParam(name = "toped", defaultValue = "false") Boolean toped, @PathVariable("roler") String roler) {
		Timestamp nowstamp = new Timestamp(new Date().getTime());
		DlNotification notification = new DlNotification();
		notification.setTitle(title);
		notification.setDetail(detail);
		notification.setCreatetime(nowstamp);
		notification.setUpdatetime(nowstamp);
		notification.setToped(toped);
		DlLoginuser user = userService.findByName(getPrincipal());
		notification.setUser(user);
		notificationService.save(notification);
		return "redirect:/" + roler + "/notification?current=1";
	}
	@RequestMapping("/morenews")
	public String moreNews(@RequestParam(name="current", defaultValue="1")Integer current, Model model, HttpSession session) {
		Page<DlNotification> notifications = notificationService.findAllPage(current-1, 25);
		DlLoginuser user = userService.findByName(getPrincipal());
		Integer currentPage = current != null ? current : 0;
		model.addAttribute("duser", user);
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("pagecount", notifications.getTotalPages());
		model.addAttribute("notifications", notifications.getContent());
		model.addAttribute("title", "动力燃油部");
		return "morenews_base";
	}
}
